<?php

require_once 'database.php';
require_once 'common.php';

$intProjectId = isset($_GET['id']) ?htmlspecialchars($_GET['id']) : 0;

$project = getProjectFromId($intProjectId);

function getProjectFromId($intProjectIdParam){
	
	//Get the project from the database based on the ID
	$objProject = array();  //This will sit in memory waiting to be referenced by the HTML
	$objProject['project_id'] = $intProjectIdParam;
	$objProject['project_name'] = "New Project";
	$objProject['start_date'] = date(JQUERY_DATEFORMAT);
	$objProject['end_date'] = date(JQUERY_DATEFORMAT);
	$objProject['iteration_length'] = 14;
	$objProject['iterations'] = array();
	$objProject['stories'] = array();
	
	//If the value of the project id is 0, then we are creating a new project
	if($intProjectIdParam != 0){
		$dbResult = mysql_query("SELECT * FROM project WHERE project_id = $intProjectIdParam");
		$row = mysql_fetch_array($dbResult);

		$objProject['project_id'] = $row['project_id'];
		$objProject['project_name'] = $row['project_name'];
		$objProject['iteration_length'] = $row['iteration_length'];
		$objProject['start_date'] = $row['start_date'];
		$objProject['end_date'] = $row['end_date'];
		
		//Get the iterations
		$iterResult = mysql_query("SELECT * FROM iteration WHERE project_id=$intProjectIdParam");
		
		while($row = mysql_fetch_array($iterResult)){
			$objIteration = array();
			$objIteration['iteration_id'] = $row['iteration_id'];
			$objIteration['start_date'] = $row['start_date'];
			$objIteration['end_date'] = $row['end_date'];
			
			//Add to the list of arrays we have
			array_push($objProject['iterations'], $objIteration);
		}
		
		//Get the stories
		$storyResult = mysql_query("SELECT s.story_id AS story_id, s.story_name AS story_name FROM project p ".
								   "JOIN iteration i ON p.project_id = i.project_id ".
								   "JOIN story s ON i.iteration_id = s.iteration_id");
		
		while($row = mysql_fetch_array($storyResult)){
			$objStory = array();
			$objStory['story_id'] = $row['story_id'];
			$objStory['story_name'] = $row['story_name'];
			
			//Add to the list of stories
			array_push($objProject['stories'], $objStory);
		}
		
	}
	
	return $objProject;
}

function getListOfIterationsForProject(){
	
	$intProjectId = isset($_GET['id']) ?htmlspecialchars($_GET['id']) : 0;
	
	//Get iterations for user
	$arrIterations = array();

	$dbResult = mysql_query("SELECT * FROM iteration WHERE project_id=$intProjectId")
	or die("Problem reading table: " . mysql_error());

	while($row = mysql_fetch_array($dbResult)){
		$objIteration = array();
		$objIteration['iteration_id'] = $row['iteration_id'];
		$objIteration['project_id'] = $row['project_id'];

		array_push($arrIterations, $objIteration);
	}

	return $arrIterations;
}

function getListOfStoriesForProject(){

	$intProjectId = isset($_GET['id']) ?htmlspecialchars($_GET['id']) : 0;

	//Get iterations for user
	$arrIterations = getListOfIterationsForProject();
	$arrStories = array();

	$dbResult = mysql_query("SELECT * FROM iteration WHERE project_id=$intProjectId")
	or die("Problem reading table: " . mysql_error());

	foreach($arrIterations as $iteration){
		$id = $iteration['iteration_id'];
		$dbResult = mysql_query("SELECT * FROM story WHERE iteration_id=(SELECT iteration_id FROM iteration WHERE iteration_id='$id')")
		or die("Problem reading table: " . mysql_error());
		
	while($row = mysql_fetch_array($dbResult)){
		$objStory = array();
		$objStory['iteration_id'] = $row['iteration_id'];
		$objStory['story_id'] = $row['story_id'];
		$objStory['story_name'] = $row['story_name'];

		array_push($arrStories, $objStory);
	}
	}

	return $arrStories;
}


